import { Injectable } from '@nestjs/common';
import { CreateTradeDto } from './dto/create-trade.dto';
import { Repository } from 'typeorm';
import { Trade } from './entities/trade.entity';
import { formatError, formatSuccess } from 'src/util';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class TradeService {
  constructor(
    @InjectRepository(Trade)
    private readonly TradeRepository: Repository<Trade>,
  ) {}

  create(createTradeDto: CreateTradeDto) {
    return 'This action adds a new trade';
  }

  // 列表
  async findAll(params) {
    const datas = await this.TradeRepository.find();
    console.log('datas: ', datas);
    return formatSuccess(datas);
  }

  // 删除所有成交记录
  async delWeiTuos() {
    const datas = await this.TradeRepository.find();
    const arr = datas.map(({ id }) => this.TradeRepository.delete({ id }));
    const res = await Promise.allSettled(arr);
    const success = res.every((item) => item.status === 'fulfilled');
    return success ? formatSuccess({ success }) : formatError({ msg: '删除失败' });
  }
}
